Modeling binary outcomes (e.g., successes/failures) across multiple independent trials.
General Principles
To model the relationship between a binary dependent variable—e.g., counts of successes/failures, yes/no, or 1/0—and one or more independent variables, we can use a Binomial model.
This is the first model for which we need a link function: e.g., the logit function. The logit link function converts the linear combination of predictor variables into probabilities, making it suitable for modeling the probability of binary outcomes. It helps to estimate the relationship between predictors and the probability of success, ensuring that model predictions fall within the bounds of the binomial distribution’s success parameter \in(0,1).
Example
Below is an example code snippet that demonstrates Bayesian binomial regression using the BayesForge (BF) package. The data consist of one binary dependent variable (pulled_left), which represents which lever each chimpanzee pulled in an experimental setup. The goal is to evaluate the probability of pulling the left side. This example is based on McElreath (2018).
from BayesForge import bf# setup platform------------------------------------------------m = bf(platform='cpu')# import data ------------------------------------------------# Importfrom importlib.resources import filesdata_path = m.load.chimpanzees(only_path =True)m.data(data_path, sep=';')m.data_to_model(['pulled_left'])# Define model ------------------------------------------------def model(pulled_left): alpha = m.dist.normal( 0, 10, name ='alpha') m.dist.binomial(total_count =1, logits=alpha, obs=pulled_left)# Run sampler ------------------------------------------------m.fit(model, progress_bar=False)# Diagnostic ------------------------------------------------m.summary()
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
mean
sd
eti89_lb
eti89_ub
ess_bulk
ess_tail
r_hat
mcse_mean
mcse_sd
alpha
0.32
0.09
0.17
0.47
1282.14
1720.67
1.0
0.0
0.0
library(BayesForge)# setup platform------------------------------------------------m=importBF(platform='cpu')# import data ------------------------------------------------m$data(m$load$chimpanzees(only_path = T), sep=';')m$data_to_model(list('pulled_left')) # Send to model (convert to jax array)# Define model ------------------------------------------------model <-function(pulled_left){# Parameters priors distributions alpha = m$dist$normal( 0, 10, name ='alpha')# Likelihood m$dist$binomial(total_count =1L, logits = alpha, obs = pulled_left, name ='pulled_left')}# Run MCMC ------------------------------------------------m$fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m$summary() # Get posterior distribution
usingPyBayesForge# Setup device------------------------------------------------m =importBF(platform="cpu")# Import Data & Data Manipulation ------------------------------------------------# Importdata_path = m.load.chimpanzees(only_path =true)m.data(data_path, sep=';')# Define model ------------------------------------------------@BFfunctionmodel(pulled_left) a = m.dist.normal( 0, 10, shape=(1,), name ="a") m.dist.binomial(total_count =1, logits=a[0], obs=pulled_left)end# Run mcmc ------------------------------------------------m.fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
Mathematical Details
Bayesian formulation
We can express the Bayesian Binomial regression model including prior distributions as follows:
Y_i \sim \text{Binomial}(N_i, p_i)
logit(p_i) = \alpha + \beta X_i
\alpha \sim \text{Normal}(0,1)
\beta \sim \text{Normal}(0,1)
Where:
Y_i is the count of successes for observation i (often a binary 0 or 1).
N_i is the count of trials for observation i (1 in the case of binary outcomes, as in the example for total_count above).
p_i is the probability of success (0 < p_i < 1) for observation i, the probability of a success.
logit(p_i) is the log-odds of success, calculated as the log of the odds ratio of success. Through this link function, the relationship between the independent variables and the log-odds of success is modeled linearly, allowing us to interpret the effect of each independent variable on the log-odds of success for observation i.
\beta and \alpha are the regression coefficient and intercept, respectively.
Model validation tests
Comparison with Stan: 4.Binomial.py fits the same model with BayesForge and Stan and compares the posterior distributions (KL divergence).
Data generative model and inference model pipeline: 4.Binomial.py simulates data from the model with known parameters (data generative model), fits the inference model to each simulated dataset and checks that the parameters are recovered.
Comparison with Stan (indexed predictors): 5.Binomial with indices.py fits the same model with BayesForge and Stan and compares the posterior distributions (KL divergence).
Data generative model and inference model pipeline: 5.Binomial with indices.py simulates data from the model with known parameters (data generative model), fits the inference model to each simulated dataset and checks that the parameters are recovered.
Plot: data generative model and inference model pipeline (4.Binomial.py)
Plot: comparison with Stan (indexed predictors) (5.Binomial with indices.py)
Plot: data generative model and inference model pipeline (5.Binomial with indices.py)
Notes
Note
We can apply multiple variables similarly to chapter 2.
We can apply interaction terms similarly to chapter 3.
We can apply categorical variables similarly to chapter 4.
Below is an example code snippet demonstrating a Bayesian binomial model for multiple categorical variables using the BayesForge (BF) package. The data consist of one binary dependent variable (pulled_left), which represents which side individuals pulled, and three independent variables (actor, side, cond). The goal is to evaluate, for each individual, the probability of pulling the left side, accounting for whether the individual is left-handed or right-handed, as well as for the different conditions.